package com.lottery.material.service.admin.article.impl;

import com.lottery.material.dao.article.ArticleChargePricingConfigMapper;
import com.lottery.material.dao.conf.UserLevelDictMapper;
import com.lottery.material.enumeration.common.CommonEnum;
import com.lottery.material.pojo.article.ArticleChargePricingConfig;
import com.lottery.material.pojo.page.Page;
import com.lottery.material.pojocustom.article.ArticleChargePricingConfigCustom;
import com.lottery.material.service.admin.article.ArticleChargePricingConfigService;
import com.lottery.material.util.ArrayUtil;
import com.lottery.material.util.CalendarUtil;
import com.lottery.material.util.EnumUtil;
import com.lottery.material.util.ListUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Description：文章收费定价配置服务接口实现类
 * Created by 朱正磊 on 2019-04-13 15:07
 */
@Service("adminArticleChargePricingConfigService")
public class ArticleChargePricingConfigServiceImpl implements ArticleChargePricingConfigService {

    private final ArticleChargePricingConfigMapper articleChargePricingConfigMapper;

    private final UserLevelDictMapper userLevelDictMapper;

    @Autowired
    public ArticleChargePricingConfigServiceImpl(ArticleChargePricingConfigMapper articleChargePricingConfigMapper,
                                                 UserLevelDictMapper userLevelDictMapper) {
        this.articleChargePricingConfigMapper = articleChargePricingConfigMapper;
        this.userLevelDictMapper = userLevelDictMapper;
    }

    /**
     * description：新增一条数据
     * user 朱正磊
     * time 2019-04-20 15:14
     * @param articleChargePricingConfig 对象
     * @return 返回持久化操作数据库状态值
     */
    @Override
    public Integer add(ArticleChargePricingConfig articleChargePricingConfig) {
        articleChargePricingConfig.setCreateTime(new Date());
        articleChargePricingConfig.setIsDelete((Integer) CommonEnum.IS_DELETE_FALSE.getValue());
        return articleChargePricingConfigMapper.insertSelective(articleChargePricingConfig);
    }

    /**
     * description：根据主键物理删除
     * user 朱正磊
     * time 2019-04-20 15:13
     * @param id 主键
     * @return 返回持久化操作数据库状态值
     */
    @Override
    public Integer deleteById(Integer id) {
        return articleChargePricingConfigMapper.deleteByPrimaryKey(id);
    }

    /**
     * description：根据主键查询唯一数据
     * user 朱正磊
     * time 2019-04-20 15:15
     * @param id 主键
     * @return 返回唯一数据
     */
    @Override
    public ArticleChargePricingConfig findById(Integer id) {
        return articleChargePricingConfigMapper.selectByPrimaryKey(id);
    }

    /**
     * description：根据主键修改一条数据
     * user 朱正磊
     * time 2019-04-20 15:16
     * @param articleChargePricingConfig 对象
     * @return 返回持久化操作数据库状态值
     */
    @Override
    public Integer updateById(ArticleChargePricingConfig articleChargePricingConfig) {
        return articleChargePricingConfigMapper.updateByPrimaryKeySelective(articleChargePricingConfig);
    }

    /**
     * description：查询扩展类分页列表
     * user 朱正磊
     * time 2019-04-17 17:01
     * @param page 分页对象
     * @return 返回扩展类分页列表数据
     */
    @Override
    public List<ArticleChargePricingConfigCustom> findCustomListPage(Page page) {
        List<ArticleChargePricingConfigCustom> list = articleChargePricingConfigMapper.findCustomListPage(page);
        list.forEach(this::setCustom);
        return list;
    }

    /**
     * description：根据主键查询唯一扩展类数据
     * user 朱正磊
     * time 2019-04-17 17:03
     * @param id 主键
     * @return 返回唯一扩展类数据
     */
    @Override
    public ArticleChargePricingConfigCustom findCustomById(Integer id) {
        ArticleChargePricingConfigCustom custom = articleChargePricingConfigMapper.findCustomById(id);
        this.setCustom(custom);
        return custom;
    }

    /**
     * description：设置扩展类的扩展属性
     * user 朱正磊
     * time 2019-04-17 17:48
     * @param custom 扩展类对象
     */
    private void setCustom(ArticleChargePricingConfigCustom custom) {
        //创建时间
        custom.setCreateTimeFormat(CalendarUtil.dateToString(custom.getCreateTime()));
        //是否删除（1：否；2：是）
        custom.setIsDeleteDesc(EnumUtil.getDescriptionByCodeValue(CommonEnum.IS_DELETE, custom.getIsDelete(), CommonEnum.class));
        //用户等级名称
        String userLevelName = userLevelDictMapper.findByUserLevel(custom.getUserLevel()).getUserLevelName();
        custom.setUserLevelName(userLevelName);
    }

    /**
     * description：根据用户等级查询唯一数据
     * user 朱正磊
     * time 2019-05-20 13:28
     * @param userLevel 用户等级
     * @return 返回唯一数据
     */
    @Override
    public ArticleChargePricingConfig findByUserLevel(Integer userLevel) {
        return articleChargePricingConfigMapper.findByUserLevel(userLevel);
    }

    /**
     * description：批量新增
     * user 朱正磊
     * time 2019-04-20 15:14
     * @param articleChargePricingConfig 对象
     * @param userLevels 用户等级数组
     * @return 返回持久化操作数据库状态值
     */
    @Override
    public Integer addBatch(ArticleChargePricingConfig articleChargePricingConfig, Integer[] userLevels) {
        List<ArticleChargePricingConfig> list = new ArrayList<>();
        for (Integer userLevel : userLevels) {
            ArticleChargePricingConfig config = new ArticleChargePricingConfig();
            config.setCreateTime(new Date());
            config.setIsDelete((Integer) CommonEnum.IS_DELETE_FALSE.getValue());
            config.setUserLevel(userLevel);
            config.setMaximumPrice(articleChargePricingConfig.getMaximumPrice());
            config.setMinimumPrice(articleChargePricingConfig.getMinimumPrice());
            list.add(config);
        }
        return articleChargePricingConfigMapper.addBatch(list);
    }

    /**
     * description：根据主键集合查询多个数据
     * user 朱正磊
     * time 2019-05-20 16:38
     * @param ids 主键集合
     * @return 返回多个数据
     */
    @Override
    public List<ArticleChargePricingConfig> findByIds(List<Integer> ids) {
        return articleChargePricingConfigMapper.findByIds(ids);
    }

    /**
     * description：批量修改
     * user 朱正磊
     * time 2019-05-20 17:13
     * @param ids 主键以,拼接的字符串
     * @param minimumPrice 最低价格
     * @param maximumPrice 最高价格
     * @return 返回持久化操作数据库状态值
     */
    @Override
    public Integer updateByIds(String ids, Integer minimumPrice, Integer maximumPrice) {
        Map<String, Object> dataMap = new HashMap<>();
        dataMap.put("idList", ListUtil.getIdList(ids));
        dataMap.put("minimumPrice", minimumPrice);
        dataMap.put("maximumPrice", maximumPrice);
        return articleChargePricingConfigMapper.updateByIds(dataMap);
    }

}
